﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;
using System.Text;
using Cosmetic;

namespace WebApp.Controls
{
    public partial class UserComments : System.Web.UI.UserControl
    {
        private ProductManager m_ProductManager = new ProductManager();
        private CommentManager m_CommentManager = new CommentManager();

        private Cosmetic.DataEntity.Product m_Product = null;

        //单个商品信息
        public Cosmetic.DataEntity.Product TheProduct
        {
            get { return m_Product; }
            set { m_Product = value; }
        }

        private User SignInUser = null;

        //登陆人信息
        public User TheSignInUser
        {
            get
            {
                return this.SignInUser;
            }
            set
            {
                SignInUser = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (m_Product != null)
            {
                BindComment(m_Product);
            }

            if (this.SignInUser == null)
            {
                ltlUserName.Text = "匿名用户";
            }
            else
            {
                ltlUserName.Text = SignInUser.UserName;
                txtUserEmail.Text = SignInUser.Email;
            }
        }

        private void BindComment(Cosmetic.DataEntity.Product aProduct)
        {
            List<Comment> listComment = this.m_CommentManager.GetAllCommentByProduct(aProduct);//GetCommentByProduct(aProduct, 3);
            this.labAllComment.Text = listComment.Count.ToString();//显示总的评论数。
            this.labAllComment2.Text = listComment.Count.ToString();//显示总的评论数。
            if (listComment.Count == 0)
            {
                LabelCommentbyUsers.Text = "暂时还没有用户评论。";
            }
            else
            {
                LabelCommentbyUsers.Text = "目前已有" + listComment.Count.ToString() + "名用户对该商品进行了评论。";
            }
            if ((listComment.Count % 3) == 0)
            {
                LabelCountPager.Text = Convert.ToInt32((listComment.Count / 3)).ToString();
            }
            else
            {
                LabelCountPager.Text = Convert.ToInt32((listComment.Count / 3)+1).ToString();
            }
           
            this.lvCommend.DataSource = listComment; //显示评论内容。
            this.lvCommend.DataBind();
        }

        protected void btnSubmitComment_Click(object sender, ImageClickEventArgs e)
        {
            if (this.SignInUser == null)
            {
                Cosmetic.WebGeneric.WebMessageBox("您还未登陆！只有登陆了，才能发表评论。", this.Page);
                return;
            }
            //add comment
            Comment NewComment = new Comment();
            NewComment.CommentId = Guid.NewGuid();
            NewComment.ProductId = m_Product.ProductId; //new Guid(Request.QueryString["PID"]);
            NewComment.UserId = this.SignInUser.UserId;
            NewComment.Content = this.txtCommentContent.Text.Trim();
            NewComment.CreateTime = DateTime.Now;
            NewComment.BigCategory = m_CommentManager.GetProductsBigCategory(m_Product.ProductId);
            NewComment.PurchaseChannelsCategoryId = new Guid("759dc19a-c30f-4425-8160-205ea6020cbf");
            NewComment.IsSample = true;

            switch (RadioButtonList1.SelectedItem.Text)
            {
                case "较差": { NewComment.Score = 1; }; break;
                case "一般": { NewComment.Score = 2; }; break;
                case "较好": { NewComment.Score = 3; }; break;
                case "很好": { NewComment.Score = 4; }; break;
                case "非常好": { NewComment.Score = 5; }; break;
                default: NewComment.Score = 5;
                    break;
            }
            NewComment.Tag = "";
            //NewComment.User = this.SignInUser;


            m_CommentManager.Insert(NewComment);
            this.BindComment(this.m_ProductManager.GetProductById(m_Product.ProductId));

            this.txtCommentContent.Text = "";
            WebGeneric.WebMessageBox("添加成功!", this.Page);
            return;
            //Response.Redirect(Request.Url.ToString());
            //Server.Transfer(Request.Url.ToString());

            //WebGeneric.WebMessageBox("添加评论成功!", this.Page);
            //return;
        }

        protected void lvCommend_DataBound(object sender, EventArgs e)
        {
            for (int j = 0; j < this.lvCommend.Items.Count; j++)
            {
                ControlCollection m_controls = this.lvCommend.Items[j].Controls;
                foreach (var item in m_controls)
                {
                    Label lab = item as Label;
                    if (lab != null)
                    {
                        switch (lab.Text)
                        {
                            case "1": { lab.Text = "较差"; }; break;
                            case "2": { lab.Text = "一般"; }; break;
                            case "3": { lab.Text = "较好"; }; break;
                            case "4": { lab.Text = "很好"; }; break;
                            case "5": { lab.Text = "非常好"; }; break;
                            default: lab.Text = "较好";
                                break;
                        }
                    }
                }
            }
        }

        protected void lvCommend_PagePropertiesChanged(object sender, EventArgs e)
        {
            BindComment(m_Product);
        }

      
    }//end class
}